<?php
/**
 * author: JiangJun
 * CreateTime:2021/6/29 16:08
 */

/**
 * 发送消息
 */
$exchangeName = 'demo';
$routeKey = 'hello';
$message = 'Hello World!';
// 建立TCP连接
$connection = new AMQPConnection([
    'host' => '192.168.10.10',
    'port' => '5672',
    'vhost' => '/',
    'login' => 'admin',
    'password' => '123456'
]);
$connection->connect() or die("Cannot connect to the broker!\n");
try {
    $channel = new AMQPChannel($connection);

    $exchange = new AMQPExchange($channel);
    $exchange->setName($exchangeName);
    $exchange->setType(AMQP_EX_TYPE_DIRECT);
    $exchange->declareExchange();

    echo 'Send Message: ' . $exchange->publish($message, $routeKey) . "\n";
    echo "Message Is Sent: " . $message . "\n";
} catch (AMQPConnectionException $e) {
    var_dump($e);
}
$connection->disconnect();// 断开连接